package com.android.music.prolyric;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class setting_player extends Activity implements OnClickListener
{
	TextView pcolorSong,pcolorSinger,pcolorId,pcolorLyric,pcolorTime;
	ImageView pbgr;
	AmbilWarnaDialog dialog;
	SharedPreferences shareColor;
	SharedPreferences.Editor editor;
	Button btnOk,btnCancel;	 
	int colorSong,colorSinger,colorId,colorLyric,colorTime,imgBgr;
	int defaultColor = 0xffffffff;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_player);
		pcolorSong = (TextView)findViewById(R.id.pcolorSong);
		pcolorSinger = (TextView)findViewById(R.id.pcolorSinger);
		pcolorId = (TextView)findViewById(R.id.pcolorIdSong);
		pcolorLyric = (TextView)findViewById(R.id.pcolorLyric);
		pcolorTime = (TextView)findViewById(R.id.pcolorTime);
		pbgr = (ImageView)findViewById(R.id.imgPlayer);
		btnOk = (Button)findViewById(R.id.btnOk);
		btnCancel = (Button)findViewById(R.id.btnCancel);
		
		pcolorId.setOnClickListener(this);
		pcolorLyric.setOnClickListener(this);
		pcolorSinger.setOnClickListener(this);
		pcolorSong.setOnClickListener(this);
		pcolorTime.setOnClickListener(this);
		pbgr.setOnClickListener(this);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		
		SharedPreferences prefs = getSharedPreferences("playerColor", MODE_PRIVATE);
		
		colorSong= (int) prefs.getInt("songcolor",defaultColor);
		colorSinger= (int) prefs.getInt("singercolor",defaultColor);
		colorId=(int) prefs.getInt("idcolor",defaultColor);
		colorLyric= (int) prefs.getInt("lyriccolor",defaultColor);
		colorTime=(int) prefs.getInt("timecolor",defaultColor);
		imgBgr = (int) prefs.getInt("pbgr",R.drawable.girl);
		
		pcolorId.setBackgroundColor(colorId);
		pcolorLyric.setBackgroundColor(colorLyric);
		pcolorSinger.setBackgroundColor(colorSinger);
		pcolorSong.setBackgroundColor(colorSong);
		pcolorTime.setBackgroundColor(colorTime);
		
		pbgr.setBackgroundResource(imgBgr);
		
		shareColor = getSharedPreferences("playerColor", MODE_PRIVATE);
		editor = shareColor.edit();
		
		
	}
	public void onClick(View v) 
	{
		switch (v.getId()) 
		{
		case R.id.pcolorIdSong:
			dialog = new AmbilWarnaDialog(setting_player.this,colorId, new OnAmbilWarnaListener() {
				
				public void onOk(AmbilWarnaDialog dialog, int color) {
					pcolorId.setBackgroundColor(color);
					colorId=color;
				}
				
				public void onCancel(AmbilWarnaDialog dialog) {
					// TODO Auto-generated method stub				
				}
			});
			dialog.show();
			
			break;
		case R.id.pcolorSong:
			dialog = new AmbilWarnaDialog(setting_player.this,colorSong, new OnAmbilWarnaListener() {
				
				public void onOk(AmbilWarnaDialog dialog, int color) {
					pcolorSong.setBackgroundColor(color);
					colorSong=color;					
				}
				
				public void onCancel(AmbilWarnaDialog dialog) {
					// TODO Auto-generated method stub				
				}
			});
			dialog.show();
			
			break;
			
		case R.id.pcolorSinger:
			dialog = new AmbilWarnaDialog(setting_player.this,colorSinger, new OnAmbilWarnaListener() {
				
				public void onOk(AmbilWarnaDialog dialog, int color) {
					pcolorSinger.setBackgroundColor(color);
					colorSinger=color;					
				}
				
				public void onCancel(AmbilWarnaDialog dialog) {
					// TODO Auto-generated method stub				
				}
			});
			dialog.show();
			
			break;
			
		case R.id.pcolorLyric:
			dialog = new AmbilWarnaDialog(setting_player.this,colorLyric, new OnAmbilWarnaListener() {
				
				public void onOk(AmbilWarnaDialog dialog, int color) {
					pcolorLyric.setBackgroundColor(color);
					colorLyric=color;
					
				}
				
				public void onCancel(AmbilWarnaDialog dialog) {
					// TODO Auto-generated method stub				
				}
			});
			dialog.show();
			
			break;
			
		case R.id.pcolorTime:
			dialog = new AmbilWarnaDialog(setting_player.this, colorTime, new OnAmbilWarnaListener() {
				
				public void onOk(AmbilWarnaDialog dialog, int color) {
					pcolorTime.setBackgroundColor(color);
					colorTime=color;
					
				}
				
				public void onCancel(AmbilWarnaDialog dialog) {
					// TODO Auto-generated method stub				
				}
			});
			dialog.show();
			
			break;
			
		case R.id.imgPlayer:
			startActivityForResult(new Intent(setting_player.this, setting_background.class),1000);	
			break;
		case R.id.btnOk:
			editor.putInt("idcolor",colorId);
			editor.putInt("songcolor", colorSong);
			editor.putInt("singercolor", colorSinger);
			editor.putInt("lyriccolor", colorLyric);
			editor.putInt("timecolor", colorTime);
			editor.putInt("pbgr",imgBgr);
			editor.commit();
			Toast.makeText(setting_player.this, "Save data succeesful", Toast.LENGTH_LONG);
			Intent broadcast = new Intent();
			broadcast.setAction("LOADSETTING");
			broadcast.putExtra("playerSetting","ok");
			getBaseContext().sendBroadcast(broadcast);
			break;
		case R.id.btnCancel:
			finish();
			break;

		default:
			break;
		}
		
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if(requestCode==1000)
			if(resultCode==RESULT_OK)
			{
				imgBgr = Integer.parseInt(data.getData().toString());				
				pbgr.setBackgroundResource(imgBgr);
			}
	}
	
	
	
}
